<?php
	$html->addJUI();
    if(isset($additionalCategories) && !@$this->data['Listing']['additional_categories'])
    {
      $this->data['AdditionalCategory'] = $additionalCategories;
      $form->data =  $this->data;
    }
	$this->pageTitle = __('Edit Listing',true);
	$tagData = "";
	if(isset($this->data['Listing']['tags']))
	{
		$tagData = Set::extract('{n}.tag',$this->data['Listing']['tags']);
		$tagData = implode(',',$tagData);
	}
	echo $form->create('Listing',array('url'=>'/'.$this->params['url']['url'],'type'=>'file','id'=>'listingFrom'));
	$assetsAddHtml = $form->input("asset_inputs"); 
	ob_start();
	$form->defaultEmpty = __("-Select-");	
	if($isOwnerEdit || @$this->params['admin'])
	{
		echo $this->element("listing_forms/select_category",array('addPage'=>false));
	}
	echo "<div id=\"listing_fields_input\">";
		echo $this->element('listing_add_form_fields',array('listingDefaultFields'=>$listingDefaultFields));
	echo "</div>";
	//show tag field if tag config is enabled by admin.
	if($isTagEnabled == true)
	{
		echo $form->input('Tag.tag',array('label'=>'Tags','value'=>$tagData));
	}
	$infoFields = ob_get_clean();
	$tabsHtml = array();
	$tabsHtml[] = array(__("Main Information"),$infoFields,array("container_id"=>"main_info"));
	$infoFields = "";
	
?>

<?php
$imagesContent = "";
//Images
if($showImageTab){
ob_start();	
?>
<div>
	
	<?php echo $this->element('assets_on_edit_page');?>	
	
	<div>
		<?php echo $assetsAddHtml?>
	</div>
</div>

<?php 

$imagesContent = ob_get_clean();
$tabsHtml[] = array(__("Images"),$imagesContent,array("container_id"=>"images_content"));
?>
<?php } ?>
<?php if(isset($this->params['admin']) && $this->params['admin']){
		ob_start();
	?>
	
	<iframe id="meta_frame" height="600px" frameborder="0" scrolling="auto" width="100%" src="<?php echo $html->url(array('controller'=>"meta_information","action"=>"setInfo",'Listing',$listingData['Listing']['id'],'iframe'=>1))?>">
		
	</iframe>
<?php 
	$tabsHtml[] = array(__("Meta Information"),ob_get_clean(),array("container_id"=>"meta_info"));
}?>
<?php if(count($tabsHtml) > 1){
	foreach($tabsHtml as $tab)
	{
		call_user_func_array(array($tabs,'addtab'), $tab);
	}
	echo $tabs->show();	
}else{?>
	<?php 
		echo $tabsHtml[0][1];
		echo $imagesContent;
	?>
<?php } ?>	
<?php 
echo "<div class='upload_filesize_limit_msg'>";
echo sprintf(__("The total size of all the files uploaded should be less than %s", true), ((ini_get("post_max_size") < ini_get("upload_max_filesize"))?ini_get("post_max_size"):ini_get("upload_max_filesize")));
echo "</div><div class='clear'></div>";
?>
<?php echo $form->end(__("Save"))?>
<?php 
if(cr('ListingConfig.allow_listing_delete'))
{echo '<span class="delete_listing">'.$html->link("Delete",array('action'=>"delete",$listingData['Listing']['id']),array('class'=>""),__("Are you sure you want to delete your listing?")).'</span>';}?>
<script>
	<?php if(isset($maxImageCount) && $maxImageCount!== false){?>
			var $maxLimit = <?php echo $maxImageCount;?>;
			var countCheck = function(){
				if($maxLimit === 0 )
				{
					$(this).parent().remove();
					return;
				}else if($maxLimit === 1 && $('.galleryImage').length > 0)
				{
					$(this).parent().hide();
					return;
				}else
				{
					$(this).parent().show();
				}
        
				var total = $(this).siblings('div.fieldWrap').length;
				total += $('.galleryImage').length;
				if(total >= $maxLimit)
				{
					$(this).find(".addAnotherField").hide();
				}else{
					$(this).find(".addAnotherField").show();
				}
			};
		$(document).ready(function(){
			$('#asset_inputs_parent_field_add_more').parent().bind('afterTemplateInsert',countCheck);
      if($('#asset_inputs_parent_field_add_more').length)
			{
          countCheck.call($('#asset_inputs_parent_field_add_more').parent()[0]);
      }
		});
	<?php } ?>
	<?php if(isset($this->params['admin']) && $this->params['admin']){?>
		$(document).ready(function(){
			var img = $('<img />').attr("src",AJAX_LOADING_IMG);
			img = $('<div></div>').html(img);
			img.hide();
			$(img).insertBefore( '#listing_fields_input' );
			var currentCategory = "<?php echo $listingData['Listing']['list_category_id']?>";
			var categoryChangeWarngDone = false;
			$("#ListingListCategoryId").change(function(){
				if(!$(this).val())
				{
					alert("<?php echo __("Please select a category")?>");
				}else
				{
					if(categoryChangeWarngDone == false)
					{
						
						if(confirm("<?php echo __("Are you sure you want to change category? Upon saving value of fields will be overwritten.")?>") == false)
						{
							$("#ListingListCategoryId").val(currentCategory);
							return;
						}else
						{
							categoryChangeWarngDone = true;
						}	
					}
					img.show();
					$('#listing_fields_input').hide();
					$('#listing_fields_input').load('<?php echo $html->url(array("action"=>"reloadForm",$listingData['Listing']['id']))?>',
							{categoryId:$(this).val()},function(){
								$('#listing_fields_input').show();
								img.hide();
							});
				}
			})
			var fn = function(){
					if($('#meta_frame').contents().find('body').length)	
						$('#meta_frame').contents().find('body').css('width','80%');
					else
						setTimeout(fn,3000);
			};
			setTimeout(fn,3000);
		});
	<?php } ?>		
</script>